package com.feinno.urcs.group.interop.service;

import com.feinno.urcs.group.api.FXUserIndexService;
import com.feinno.urcs.group.interop.entity.FXCatalogIndex;
import com.feinno.urcs.group.interop.entity.FXMobileNo;
import com.feinno.urcs.group.interop.IUserIndexService;
import com.feinno.urcs.types.MobileNo;
import org.helium.framework.annotations.ServiceImplementation;
import org.helium.framework.annotations.ServiceSetter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Li.Hongbo <lihongbo@feinno.com>
 */
@ServiceImplementation
public class FXUserIndexServiceImpl implements FXUserIndexService {
	@ServiceSetter
	private IUserIndexService cats;
	private static final Logger LOGGER = LoggerFactory.getLogger(FXUserIndexServiceImpl.class);


	@Override
	public int getFxUserId(MobileNo mobileNo) throws Exception {
		FXMobileNo fxMobileNo = new FXMobileNo(mobileNo.getMobile());
		LOGGER.info(String.format("MobileNo =%s FXMobileNo=%s", mobileNo, fxMobileNo));

		FXCatalogIndex fxCatalogIndex = cats.getUserIndexByMobileNo(fxMobileNo);
		return fxCatalogIndex.getUserId();
	}

	@Override
	public int getLogicPool(MobileNo mobileNo) throws Exception {
		FXMobileNo fxMobileNo = new FXMobileNo(mobileNo.getMobile());
		LOGGER.info(String.format("MobileNo =%s FXMobileNo=%s", mobileNo, fxMobileNo));

		FXCatalogIndex fxCatalogIndex = cats.getUserIndexByMobileNo(fxMobileNo);
		return fxCatalogIndex.getLogicalPoolId();
	}

	@Override
	public int getLogicPoolByUserId(int userId) throws Exception {
		LOGGER.info(String.format("userId =%s ", userId));
		if(userId ==0){
			LOGGER.error(String.format("userId =%s ", userId));
		}

		FXCatalogIndex fxCatalogIndex = cats.getUserIndexByUserId(userId);
		return fxCatalogIndex.getLogicalPoolId();
	}

	@Override
	public int getFxSid(MobileNo mobileNo) throws Exception {

		FXMobileNo fxMobileNo = new FXMobileNo(mobileNo.getMobile());
		LOGGER.info(String.format("MobileNo =%s FXMobileNo=%s", mobileNo, fxMobileNo));

		FXCatalogIndex fxCatalogIndex = cats.getUserIndexByMobileNo(fxMobileNo);
		return fxCatalogIndex.getSid();

	}

	@Override
	public FXCatalogIndex getFXCatalogIndex(MobileNo mobileNo) throws Exception {
		FXMobileNo fxMobileNo = new FXMobileNo(mobileNo.getMobile());
		FXCatalogIndex fxCatalogIndex = cats.getUserIndexByMobileNo(fxMobileNo);
		return fxCatalogIndex;
	}

	@Override
	public FXCatalogIndex createAndGetFXCatalogIndex(MobileNo mobileNo) throws Exception {
		FXMobileNo fxMobileNo = new FXMobileNo(mobileNo.getMobile());
		LOGGER.info(String.format("MobileNo =%s FXMobileNo=%s", mobileNo, fxMobileNo));

		FXCatalogIndex fxCatalogIndex = cats.getUserIndexByMobileNo(fxMobileNo);
		if (fxCatalogIndex == null) {
			//TODO PS 开通飞信.
		}
		fxCatalogIndex = cats.getUserIndexByMobileNo(fxMobileNo);
		return fxCatalogIndex;
	}
}
